<template>
    <div>
        <el-dialog :title="cateDialog.title" :visible.sync="cateDialog.flag">
            <el-form :model="cateAdd">
                <el-form-item label="分类名称" label-width="80px">
                    <el-input v-model="cateAdd.cat_name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="父级分类" label-width="80">
                    <el-cascader :options="options" :props="props" v-model="changedata"></el-cascader>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cateDialog.flag = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import cateAddApi from '@/utils/cateApi/cateAddApi'
import cateEditApi from '@/utils/cateApi/cateEditApi'
export default {
    props: {
        cateDialog: Object,
        cateAdd: Object,
        options: Array,
        changedata: Array,
        props: Object,
        editId: Number
    },
    methods: {
        async submit() {
            // alert(111);
            if (this.cateDialog.type == 'add') {
                this.$emit('getData')
                if (this.changedata.length == 0) {
                    this.cateAdd.cat_pid = 0
                } else {
                    this.cateAdd.cat_pid = this.changedata[this.changedata.length - 1]
                }
                this.cateAdd.cat_level = this.changedata.length;
                let res = await cateAddApi(this.cateAdd);
                if (res.meta.status == 201) {
                    this.$message.success(res.meta.msg);
                    this.$emit('getCateList');
                    this.close();
                }
            } else if (this.cateDialog.type == 'edit') {
                // alert(111);
                // console.log(this.editId);
                // console.log(this.cateAdd.cat_name)
                let res = await cateEditApi(this.editId, { cat_name: this.cateAdd.cat_name });
                if (res.meta.status == 200) {
                    this.$message.success(res.meta.msg);
                    this.$emit('getCateList');
                    this.close();
                } else {
                    this.$message.error(res.meta.msg);
                }
            }
        },
        /* 关闭模态框 */
        close() {
            this.cateDialog.flag = false;
        }
    }
}
</script>

<style lang="scss" scoped>

</style>